#include "bullet.h"

Bullet::Bullet()
{
    //加载资源
    m_Bullet.load(BULLET_PATH);
    e_Bullet.load(ENEMYBULLET_PATH);

    //子弹坐标(应随着飞机坐标变化)
    m_X = GAME_WIDTH*0.5-m_Bullet.width()*0.5;//初始坐标随便设一个
    m_Y = GAME_HEIGHT;

    //子弹状态 默认空闲
    m_Free = true;

    //子弹速度
    m_Speed = BULLET_SPEED;
    e_Speed = ENEMYBULLET_SPEED;
    //子弹边框
    m_Rect.setWidth(m_Bullet.width());
    m_Rect.setHeight(m_Bullet.height());
    m_Rect.moveTo(m_X, m_Y);//把子弹移动到此位置

}

void Bullet::updatePosition()//仅动y即可
{
    //空闲状态下的子弹不需要计算坐标
    if(m_Free)
    {
        return;
    }

    //子弹向上移动
    m_Y -= m_Speed;
    m_Rect.moveTo(m_X,m_Y);

    //子弹位置超出屏幕，状态变为free
    if(m_Y <= -m_Rect.height()||m_Y <= -m_Rect.height()||m_X <= -m_Rect.width()||m_X >= GAME_WIDTH + m_Rect.width())//最上面是负的
    {
        m_Free = true;
    }
}

void Bullet::e_updatePosition()
{
    //空闲状态下的子弹不需要计算坐标
    if(m_Free)
    {
        return;
    }

    //子弹向下移动
    m_Y += m_Speed;
    m_Rect.moveTo(m_X,m_Y);

    //子弹位置超出屏幕，状态变为free
    if(m_Y >= 0)//最上面是负的
    {
        m_Free = true;
    }
}
